package com.makeunion.gdlbsdemo.activity;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.amap.api.services.weather.LocalDayWeatherForecast;
import com.amap.api.services.weather.LocalWeatherForecast;
import com.amap.api.services.weather.LocalWeatherForecastResult;
import com.amap.api.services.weather.LocalWeatherLive;
import com.amap.api.services.weather.LocalWeatherLiveResult;
import com.amap.api.services.weather.WeatherSearch;
import com.amap.api.services.weather.WeatherSearchQuery;
import com.makeunion.gdlbsdemo.R;

import java.util.ArrayList;
import java.util.List;

/**
 * 类说明
 *
 * @author renjialiang
 * @version [版本]
 * @see [相关类]
 * @since [模块]
 */
public class SearchWeatherActivity extends AppCompatActivity implements
        WeatherSearch.OnWeatherSearchListener {

    // 实况
    TextView liveTimeTxt;

    TextView liveWeatherTxt;

    TextView liveTemperatureTxt;

    TextView liveWindDirectionTxt;

    TextView liveWindPowerTxt;

    TextView liveHumidityTxt;

    // 预报
    TextView forecastTimeTxt;

    ListView forecastListView;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_weather_search);

        liveTimeTxt = (TextView)findViewById(R.id.live_time_txt);
        liveWeatherTxt = (TextView)findViewById(R.id.live_weather_txt);
        liveTemperatureTxt = (TextView)findViewById(R.id.live_temperature_txt);
        liveWindDirectionTxt = (TextView)findViewById(R.id.live_wind_direction_txt);
        liveWindPowerTxt = (TextView)findViewById(R.id.live_wind_power_txt);
        liveHumidityTxt = (TextView)findViewById(R.id.live_humidity_txt);

        forecastTimeTxt = (TextView)findViewById(R.id.forecast_time_txt);
        forecastListView = (ListView)findViewById(R.id.list_view);

        searchLiveWeather();
        searchForecastWeather();
    }

    private void searchLiveWeather() {
        WeatherSearchQuery query = new WeatherSearchQuery("北京", WeatherSearchQuery.WEATHER_TYPE_LIVE);
        WeatherSearch weathersearch=new WeatherSearch(this);
        weathersearch.setOnWeatherSearchListener(this);
        weathersearch.setQuery(query);
        weathersearch.searchWeatherAsyn();
    }

    private void searchForecastWeather() {
        WeatherSearchQuery query = new WeatherSearchQuery("北京", WeatherSearchQuery.WEATHER_TYPE_FORECAST);
        WeatherSearch weathersearch=new WeatherSearch(this);
        weathersearch.setOnWeatherSearchListener(this);
        weathersearch.setQuery(query);
        weathersearch.searchWeatherAsyn();
    }

    @Override
    public void onWeatherLiveSearched(LocalWeatherLiveResult result, int i) {
        LocalWeatherLive liveResult = result.getLiveResult();
        liveTimeTxt.setText(liveResult.getReportTime());
        liveWeatherTxt.setText(liveResult.getWeather());
        liveTemperatureTxt.setText(liveResult.getTemperature() + "°C");
        liveWindDirectionTxt.setText(liveResult.getWindDirection());
        liveWindPowerTxt.setText(liveResult.getWindPower() + "级");
        liveHumidityTxt.setText(liveResult.getHumidity() + "%");
    }

    @Override
    public void onWeatherForecastSearched(LocalWeatherForecastResult result, int i) {
        LocalWeatherForecast forecastResult = result.getForecastResult();
        forecastTimeTxt.setText(forecastResult.getReportTime());
        List<LocalDayWeatherForecast> weatherForecast = forecastResult.getWeatherForecast();
        if (weatherForecast == null) {
            return;
        }
        List<Forecast> forecastList = new ArrayList<>();
        for (LocalDayWeatherForecast forecast : weatherForecast) {
            String dayWeather = forecast.getDayWeather();
            String dayTemp = forecast.getDayTemp();
            String dayWindDirection = forecast.getDayWindDirection();
            String dayWindPower = forecast.getDayWindPower();
            String nightWeather = forecast.getNightWeather();
            String nightTemp = forecast.getNightTemp();
            String nightWindDirection = forecast.getNightWindDirection();
            String nightWindPower = forecast.getNightWindPower();

            forecastList.add(new Forecast(dayWeather, dayTemp, dayWindDirection, dayWindPower,
                    nightWeather, nightTemp, nightWindDirection, nightWindPower));
        }

        ForecastAdapter adapter = new ForecastAdapter(this, forecastList);
        forecastListView.setAdapter(adapter);
    }

    class Forecast {
        public String dayWeather;
        public String dayTemp;
        public String dayWindDirection;
        public String dayWindPower;
        public String nightWeather;
        public String nightTemp;
        public String nightWindDirection;
        public String nightWindPower;

        public Forecast(String dayWeather, String dayTemp, String dayWindDirection,
                String dayWindPower, String nightWeather, String nightTemp,
                String nightWindDirection, String nightWindPower) {
            this.dayWeather = dayWeather;
            this.dayTemp = dayTemp;
            this.dayWindDirection = dayWindDirection;
            this.dayWindPower = dayWindPower;
            this.nightWeather = nightWeather;
            this.nightTemp = nightTemp;
            this.nightWindDirection = nightWindDirection;
            this.nightWindPower = nightWindPower;
        }
    }

    class ForecastAdapter extends BaseAdapter {

        private List<Forecast> forecastList;

        private LayoutInflater inflater;

        public ForecastAdapter(Context context, List<Forecast> forecastList) {
            this.forecastList = forecastList;
            this.inflater =  LayoutInflater.from(context);
        }

        @Override
        public int getCount() {
            return forecastList == null ? 0 : forecastList.size();
        }

        @Override
        public Object getItem(int position) {
            return forecastList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View view, ViewGroup parent) {
            Forecast forecast = forecastList.get(position);
            ViewHolder h = null;
            if (view == null) {
                view = inflater.inflate(R.layout.list_item_forecast_weather, parent, false);
                h = new ViewHolder();
                h.dayWeatherTxt = (TextView)view.findViewById(R.id.day_weather_txt);
                h.dayTempTxt = (TextView)view.findViewById(R.id.day_temp_txt);
                h.dayWindDirectionTxt = (TextView)view.findViewById(R.id.day_wind_direction_txt);
                h.dayWindPowerTxt = (TextView)view.findViewById(R.id.day_wind_power_txt);
                h.nightWeatherTxt = (TextView)view.findViewById(R.id.night_weather_txt);
                h.nightTempTxt = (TextView)view.findViewById(R.id.night_temp_txt);
                h.nightWindDirectionTxt = (TextView)view.findViewById(R.id.night_wind_direction_txt);
                h.nightWindPowerTxt = (TextView)view.findViewById(R.id.night_wind_power_txt);
                view.setTag(h);
            } else {
                h = (ViewHolder)view.getTag();
            }
            h.dayWeatherTxt.setText(forecast.dayWeather);
            h.dayTempTxt.setText(forecast.dayTemp + "°C");
            h.dayWindDirectionTxt.setText(forecast.dayWindDirection);
            h.dayWindPowerTxt.setText(forecast.dayWindPower + "级");
            h.nightWeatherTxt.setText(forecast.nightWeather);
            h.nightTempTxt.setText(forecast.nightTemp + "°C");
            h.nightWindDirectionTxt.setText(forecast.nightWindDirection);
            h.nightWindPowerTxt.setText(forecast.nightWindPower + "级");
            return view;
        }
    }

    class ViewHolder {
        TextView dayWeatherTxt;
        TextView dayTempTxt;
        TextView dayWindDirectionTxt;
        TextView dayWindPowerTxt;
        TextView nightWeatherTxt;
        TextView nightTempTxt;
        TextView nightWindDirectionTxt;
        TextView nightWindPowerTxt;
    }
}
